/*
#include <gpio.h>
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-24     86188       the first version
 */
#include <rtdevice.h>
#include "include/gpio.h"
/*===========================================================================*/
G_ioHandleTypedef G_ioHandle;
/*===========================================================================*/
static void io_init(void)
{
    /* set on-board led as push-pull output*/
    rt_pin_mode(power_led_status, PIN_MODE_OUTPUT);
    rt_pin_mode(power_led, PIN_MODE_OUTPUT);

    /* set the initial state of led to be on */
    rt_pin_write(power_led_status, PIN_LOW);
    rt_pin_write(power_led, PIN_LOW);

    //init configuration
    rt_pin_mode(out_audio_power_enable, PIN_MODE_OUTPUT);
    rt_pin_write(out_audio_power_enable, PIN_LOW);

    rt_pin_mode(led_2, PIN_MODE_OUTPUT);
    rt_pin_mode(led_3, PIN_MODE_OUTPUT);
    rt_pin_write(led_2, PIN_LOW);
    rt_pin_write(led_3, PIN_LOW);

    rt_pin_mode(out_usb3_reset, PIN_MODE_OUTPUT);
    rt_pin_mode(out_usb2_reset, PIN_MODE_OUTPUT);
    rt_pin_write(out_usb3_reset, PIN_LOW);
    rt_pin_write(out_usb3_reset, PIN_LOW);

    rt_pin_mode(out_usb3_vbus, PIN_MODE_OUTPUT);
    rt_pin_mode(out_usb2_vbus, PIN_MODE_OUTPUT);
    rt_pin_write(out_usb3_vbus, PIN_LOW);
    rt_pin_write(out_usb2_vbus, PIN_LOW);

    rt_pin_mode(out_audio_usb_enable, PIN_MODE_OUTPUT);
    rt_pin_write(out_audio_usb_enable, PIN_LOW);

    rt_pin_mode(out_audio_i2s_switch, PIN_MODE_OUTPUT);
    rt_pin_write(out_audio_i2s_switch, PIN_HIGH);

    rt_pin_mode(key_onoff, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(plug_check, PIN_MODE_OUTPUT_OD);
}

static void g_io_high(uint8_t ID)
{
    rt_pin_write(ID, PIN_HIGH);
}
static void g_io_low(uint8_t ID)
{
    rt_pin_write(ID, PIN_LOW);
}
static void g_io_toggle(uint8_t ID)
{
    rt_pin_write(ID, !rt_pin_read(ID));
}

static int g_io_init(void)
{
    io_init();

    G_ioHandle.high = g_io_high;
    G_ioHandle.low = g_io_low;
    G_ioHandle.toggle = g_io_toggle;
    return 0;
}

/* automatic initialization */
INIT_BOARD_EXPORT(g_io_init);
